var productSkuBag = new Array(); //货品规格集合，用于保存已选择的货品规格
$j(document).ready(function () {
	//验证
	applyValidate($("lowestPrice"),"price");
	applyValidate($("highestPrice"),"price");
	
	searchProducts(true);
});

//重设搜索条件
function resetSearchCriteria(){
	fnResetForm("productSearchCriteria");
}

//验证函数
function validateSearchCriteria(){
	if(!validateForm($j('#productSearchCriteria').get(0))){
    	return false;
   	}
   	return true;
}


//搜索货品 isNew标示是否新的搜索,param标示是否自定义参数搜索
function searchProducts(isNew,param){
	var postData;
	var url;
	if(isNew){
		if(null==param){
			if(validateSearchCriteria()){
				//有搜索结果搜索
			    postData = $j('#productSearchCriteria :input').serializeArray();
			    url=__ctxPath+"/product/productSkuSelector.html?doAction=search&decorator=blank";
				$j("#productSkuSelectorResults").load(url,postData,searchProductsCallback);   
		    }else{
		    	return;
		    }
		}else{
			//新的搜索。全部，上架等
			//有搜索结果搜索
			if(param.status  != undefined){
				$j('#storageStatus').val(""+param.status);
			}
			if(param.categoryId  != undefined){
				$j('#storageCategoryId').val(""+param.categoryId);
			}
		    postData = $j('#productSkuSelectorResults :input').serializeArray();
			url=__ctxPath+"/product/productSkuSelector.html?doAction=search&decorator=blank&&btnSearch=1";
			$j("#productSkuSelectorResults").load(url,postData,searchProductsCallback);  
		}    
	}else{
		postData = $j('#productSkuSelectorResults :input').serializeArray();
		url=__ctxPath+"/product/productSkuSelector.html?doAction=search&decorator=blank";
		$j("#productSkuSelectorResults").load(url,postData,searchProductsCallback);  
	} 
	
}

//分页
function fnOnGoToPage(){
	searchProducts(false);
}

//搜索货品回调函数
function searchProductsCallback(){
	if(multiple)$j("#multipleSelectDiv").show();
}
//验证函数
function validateAction(){
	if(!validateForm($j('#productSearchCriteria').get(0))){
		//alert(__vaMsg.notPass);
    	return false;
   	}
   	return true;
}
//选择多个货品
function selectProductSkus(){
	if(productSkuBag.length == 0){
		alert(__FMT.productSkuSelector_pleaseSelect);
		return;
	}
	callbackFunc.call(this,productSkuBag);
	cleanupProductSkus();
}

//清空货品
function cleanupProductSkus(){
	productSkuBag = new Array();
	$j("#selectedProductSkuInfo").empty();
}

//选择货品
function selectProductSku(productSkuId){
	var productSku = {  "productSkuId":productSkuId,
						"productSkuCode":$('productSkuCode_'+productSkuId).value,
						"productSkuName":$('productSkuName_'+productSkuId).value,
						"imageUrl":$('imageUrl_'+productSkuId).value,
						"productId":$('productId_'+productSkuId).value,
						"productName":$('productName_'+productSkuId).value,
						"productCode":$('productCode_'+productSkuId).value,
						"minOrderQuantity":$('minOrderQuantity_'+productSkuId).value,
						"productImageUrl":$('productImageUrl_'+productSkuId).value,
						"orderSkuDisplayOption":$('orderSkuDisplayOption_'+productSkuId).value,
						"productKind":$('productKind_'+productSkuId).value
	};
	if(multiple){
		addProductSku(productSku);
	}else{
		callbackFunc.call(this,productSku);
	}
}
//添加货品
function addProductSku(productSku){
	
	if(!isContain(productSku.productSkuId)){
		productSkuBag.push(productSku);
		addProductSkuInfo(productSku);
	}else{
		jAlert(__FMT.productSkuSelector_alreadySelect);
	}	
}
//移除货品
function removeProductSku(productSkuId){
	var index = indexOfProductSkuBag(productSkuId);
	if(-1 != index){
		productSkuBag.remove(index);
		removeProductSkuInfo(productSkuId);
	}
}
//获取productSku在productSkubag中的index，不存在返回-1
function indexOfProductSkuBag(productSkuId){
	for(var i=0; i<productSkuBag.length; i++){
		var item = productSkuBag[i];
		if(item.productSkuId == productSkuId){
			return i;
		}
	}
	return -1;
}
//判断是否已经包含该货品
function isContain(productSkuId){
	var isExist = false;
	for(var i=0; i<productSkuBag.length; i++){
		var item = productSkuBag[i];
		if(item.productSkuId == productSkuId){
			isExist = true;
			break;
		}
	}
	return isExist;
}
//添加货品规格显示信息 productSku是一个JSONObject
function addProductSkuInfo(productSku){
	var html = '';
	html += '<li id="productSkuSelectedInfo'+productSku.productSkuId+'"><div class="first" style="width:170px;" id="productSkuSelectedInfo'+productSku.productSkuId+'">'+productSku.productSkuName+'</div>';
	html += '<div class="second"><a href="javascript:void(0)" class="del" onclick="removeProductSku('+productSku.productSkuId+')">&nbsp;</a></div>';    
	html += '</li>';
	$j("#selectedProductSkuInfo").append(html);
}
//移除货品显示信息
function removeProductSkuInfo(productSkuId){
	$j("#productSkuSelectedInfo"+productSkuId).remove();
}


/*
   *  方法:Array.remove(index)
   *  功能:删除数组元素.
   *  参数:index删除元素的下标.
   *  返回:在原数组上修改数组
   */

 Array.prototype.remove=function(index)
 {
   if(isNaN(index)||index>this.length){return false;}
   for(var i=0,n=0;i<this.length;i++)
   {
       if(this[i]!=this[index])
       {
           this[n++]=this[i]
       }
   }
   this.length-=1
 }

